import { defineStore } from 'pinia'
import { RouteRecordName } from 'vue-router'
import { RouteLocationNormalizedLoaded } from 'vue-router'

export const useTagsViewStore = defineStore('tagsView', {
  state: () => {
    return {
      visitedViews: [] as Array<RouteLocationNormalizedLoaded>,
      cachedViews: [] as Array<RouteRecordName>,
      refreshFlag: false // tag刷新的flag
    }
  },

  actions: {
    // 添加
    addView(view: RouteLocationNormalizedLoaded) {
      this.addVisitedView(view)
      this.addCachedView(view)
    },
    addVisitedView(view: RouteLocationNormalizedLoaded) {
      this.$patch(state => {
        if (state.visitedViews.some(v => v.path === view.path)) return
        state.visitedViews.push(
          Object.assign({}, view, {
            title: view.meta.title || 'no-name'
          })
        )
      })
    },
    addCachedView(view: RouteLocationNormalizedLoaded) {
      this.$patch(state => {
        if (state.cachedViews.includes(view.name as RouteRecordName)) return
        if (!view.meta.noCache && view.name) {
          state.cachedViews.push(view.name)
        }
      })
    },
    // 删除
    delView(view: KeyValue) {
      return new Promise(resolve => {
        this.delVisitedView(view)
        this.delCachedView(view)
        let data: { visitedViews: Array<RouteLocationNormalizedLoaded>, cachedViews: Array<RouteRecordName> } = {
          visitedViews: [...this.visitedViews],
          cachedViews: [...this.cachedViews]
        }
        resolve(data)
      })
    },
    // 删除标签页
    delVisitedView(view: KeyValue) {
      return new Promise(resolve => {
        this.$patch(state => {
          for (const [i, v] of state.visitedViews.entries()) {
            if (v.path === view.path) {
              state.visitedViews.splice(i, 1)
              break
            }
          }
          resolve([...state.visitedViews])
        })
      })
    },
    // 删除标签页的缓存
    delCachedView(view: KeyValue) {
      return new Promise(resolve => {
        this.$patch(state => {
          const index = state.cachedViews.indexOf(view.name)
          index > -1 && state.cachedViews.splice(index, 1)
          resolve([...state.cachedViews])
        })
      })
    },
    // 关闭其他标签页 包括缓存的
    delOthersViews(view: { path?: any; name?: any }) {
      return new Promise(resolve => {
        this.$patch(state => {
          
          state.visitedViews = state.visitedViews.filter(v => {
            return v.meta.affix || v.path === view.path
          })
          resolve([...state.visitedViews])
  
          const index = state.cachedViews.indexOf(view.name)
          if (index > -1) {
            state.cachedViews = state.cachedViews.slice(index, index + 1)
          } else {
            // if index = -1, there is no cached tags
            state.cachedViews = []
          }
  
          resolve({
            visitedViews: [...state.visitedViews],
            cachedViews: [...state.cachedViews]
          })

        })

      })
    },
    // 更新活动页面
    updateVisitedView(view: RouteLocationNormalizedLoaded) {
      this.$patch(state => {
        for (let v of state.visitedViews) {
          if (v.path === view.path) {
            v = Object.assign(v, view)
            break
          }
        }
      })
    },
    // 删除所有标签页
    delAllViews() {
      return new Promise(resolve => {
        this.$patch(state => {

          const affixTags = state.visitedViews.filter(tag => tag.meta.affix)
          state.visitedViews = affixTags

          state.cachedViews = []

          resolve({
            visitedViews: [...state.visitedViews],
            cachedViews: [...state.cachedViews]
          })
        })
      })
    },
    /**
     * 设置刷新flag
     */
    setRefreshFlag(flag: boolean) {
      this.$patch(state => {
        state.refreshFlag = flag
      })
    }
  }
})
